<?php
/**
 * @title 399. 除法求值
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String[][] $equations
     * @param Float[] $values
     * @param String[][] $queries
     * @return Float[]
     */
    function calcEquation($equations, $values, $queries) {
        /**
         * @since 2023-12-27 哈希存储，[除数][被除数] = 商
         */
        $hashArray = [];

        /**
         * @since 2023-12-27 遍历变量对数组
         */
        foreach ($equations as $k=>list($n, $m)){
            $val = $values[$k];
            $hashArray[$n][$m] = $val;
            $hashArray[$m][$n] = 1/$val;
        }

        /**
         * @since 2023-12-27 问题答案数组
         */
        $answerArray = [];

        /**
         * @since 2023-12-27 遍历查询数组
         */
        foreach ($queries as list($n, $m)){
            /**
             * @since 2023-12-27 判断$n, $m是否是已知变量字符串
             */
            if(!isset($hashArray[$n]) OR !isset($hashArray[$m])){
                $answerArray[] = -1.0;
                continue;
            } else {}

            /**
             * @since 2023-12-27 除数和被除数相等
             */
            if($n == $m){
                $answerArray[] = 1.0;
                continue;
            } else {}

            /**
             * @since 2023-12-27 记录出现的字符，防止回溯
             */
            $charHashArray = [];
            $charHashArray[$n] = true;

            /**
             * @since 2023-12-27 是否存在答案
             */
            $existsAnswer = false;

            /**
             * @since 2023-12-27 遍历能找到$n/$m
             */
            $nodeArray = [[$n,1]];
            while($nodeArray){
                /**
                 * @since 2023-12-27 下次遍历的集合
                 */
                $nextNodeArray = [];

                /**
                 * @since 2023-12-27 遍历当前集合
                 */
                foreach ($nodeArray as list($node, $v1)){
                    foreach ($hashArray[$node] as $k=>$v2){
                        /**
                         * @since 2023-12-27 找到$m
                         */
                        if($k == $m){
                            $answerArray[] = $v1*$v2;
                            $nextNodeArray = [];
                            $existsAnswer = true;
                            break 2;
                        } else {}

                        /**
                         * @since 2023-12-27 没找到$m，继续遍历
                         */
                        if(!isset($charHashArray[$k])){
                            $nextNodeArray[] = [$k, $v1*$v2];
                            $charHashArray[$k] = true;
                        } else {}
                    }
                }

                /**
                 * @since 2023-12-27 下次遍历
                 */
                $nodeArray = $nextNodeArray;
            }

            /**
             * @since 2023-12-27 未找到答案
             */
            if(!$existsAnswer){
                $answerArray[] = -1.0;
            } else {}
        }

        /**
         * @return Float[]
         */
        return $answerArray;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202312/2023.12.27_19.php
 */
$datas = [
    // [[["a","b"],["b","c"]], [2.0,3.0], [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]],
    [[["a","b"],["b","c"],["a","c"]], [2.0,3.0,6.0], [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->calcEquation(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
